package sma.iss.util;



/**
 * Utility around angles
 * 
 * @author smarsoll
 * 
 */
public class AngleUtil {

	private static final double ANGLE_360 = 360.0;

	/**
	 * 
	 * @param angle
	 * @return value between 0 and 359.999999
	 */
	public final static double reduce(double angle) {
		double result = angle;
		while (result >= ANGLE_360) {
			result = result - ANGLE_360;
		}
		while (result < 0.0) {
			result = result + ANGLE_360;
		}
		return result;
	}

	public final static double addAngle(double angle, double delta) {
		double result = angle + delta;
		return reduce(result);
	}

	public final static double distance(double angle1, double angle2) {
		double result = addAngle(angle1, -angle2);
		if (result > 180.0) {
			result = addAngle(0, -result);
		}
		return result;
	}

	public final static double middleAngle(double angle1, double angle2) {
		double result = addAngle(angle1, angle2) / 2.0;
		double oppose = addAngle(result, 180.0);
		if (distance(angle1, result) <= distance(angle1, oppose)) {
			return result;
		} else {
			return oppose;

		}
	}

	public final static double getShift(double angle1, double angle2) {
		double shift = angle2 - angle1;
		if (shift < -180.0)
			shift += 360.0;
		if (shift > 180.0)
			shift -= 360.0;
		return shift;
	}

}
